package com.example.heimashop.view.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.heimashop.base.BaseFragment;
import com.example.heimashop.databinding.FragmentHomeBinding;
import com.example.heimashop.model.Banner;
import com.example.heimashop.utils.GlideUtils;
import com.example.heimashop.utils.ToastUtils;
import com.example.heimashop.view.activity.ProductDetailActivity;
import com.example.heimashop.view.activity.SearchActivity;
import com.example.heimashop.view.adapter.FloorAdapter;
import com.example.heimashop.view.adapter.NavigationAdapter;
import com.example.heimashop.viewmodel.HomeViewModel;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

/**
 * 首页Fragment
 * 页面3: HomeFragment - 首页，包含轮播图、导航、楼层
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding> {

    private HomeViewModel viewModel;
    private NavigationAdapter navigationAdapter;
    private FloorAdapter floorAdapter;

    @Override
    protected FragmentHomeBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentHomeBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initView() {
        viewModel = new ViewModelProvider(this).get(HomeViewModel.class);

        // 初始化导航适配器
        navigationAdapter = new NavigationAdapter();
        binding.rvNavigation.setLayoutManager(new GridLayoutManager(getContext(), 5));
        binding.rvNavigation.setAdapter(navigationAdapter);

        // 初始化楼层适配器
        floorAdapter = new FloorAdapter();
        binding.rvFloors.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.rvFloors.setAdapter(floorAdapter);

        // 设置轮播图
        binding.banner.addBannerLifecycleObserver(this)
                .setIndicator(new CircleIndicator(getContext()));
    }

    @Override
    protected void initData() {
        // 观察数据
        observeData();

        // 加载数据
        viewModel.getBanners();
        viewModel.getNavigation();
        viewModel.getFloorData();
    }

    @Override
    protected void initListener() {
        // 搜索框点击
        binding.etSearch.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), SearchActivity.class);
            startActivity(intent);
        });

        // 导航点击
        navigationAdapter.setOnItemClickListener(navigation -> {
            ToastUtils.showShort(requireContext(), "点击了: " + navigation.getName());
        });

        // 楼层商品点击
        floorAdapter.setOnItemClickListener(product -> {
            ToastUtils.showShort(requireContext(), "点击了: " + product.getName());
            // 由于楼层商品没有ID,暂不跳转详情页
        });
    }

    private void observeData() {
        // 观察轮播图数据
        viewModel.bannersLiveData.observe(getViewLifecycleOwner(), banners -> {
            if (banners != null && !banners.isEmpty()) {
                setupBanner(banners);
            }
        });

        // 观察导航数据
        viewModel.navigationLiveData.observe(getViewLifecycleOwner(), navigations -> {
            if (navigations != null) {
                navigationAdapter.setData(navigations);
            }
        });

        // 观察楼层数据
        viewModel.floorsLiveData.observe(getViewLifecycleOwner(), floors -> {
            if (floors != null) {
                floorAdapter.setData(floors);
            }
        });

        // 观察错误信息
        viewModel.errorLiveData.observe(getViewLifecycleOwner(), error -> {
            if (error != null) {
                ToastUtils.showShort(requireContext(), error);
            }
        });
    }

    private void setupBanner(List<Banner> banners) {
        binding.banner.setAdapter(new BannerImageAdapter<Banner>(banners) {
            @Override
            public void onBindView(BannerImageHolder holder, Banner data, int position, int size) {
                GlideUtils.loadImage(holder.itemView.getContext(), data.getImageSrc(), holder.imageView);
            }
        });
    }
}
